package wangm.service;

import wangm.bean.ExaminationPaper;
import wangm.bean.ExaminationTopic;
import wangm.repository.ExaminationPaperRepository;
import wangm.util.StringsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 试卷serviceImp
 *
 * @author zk
 * @date 2018-02-23 10:56
 */
@Service
public class ExaminationPaperServiceImp {
    /**
     * 小题 service
     */
    @Autowired
    private ExaminationTopicServiceImp paperTopicService;
    /**
     * 试卷 repository
     */
    @Autowired
    private ExaminationPaperRepository paperRepository;
    /**
     * 历史试卷 service
     */
    @Autowired
    private ExaminationPaperHistoryServiceImp paperHistoryService;
    /**
     * 根据paperId获取该试卷
     *
     * @param paperId paperId
     * @return ExaminationPaper
     */
    public ExaminationPaper getExaminationPaperById(String paperId){
        //获取指定paper（只有本身描述性信息，无小题等信息）
        ExaminationPaper result = paperRepository.getById(paperId);
        //组合小题
        result.setTopics(paperTopicService.getTopicByPaperId(paperId));
        return result;
    }

    /**
     * 根据paperId 获取有答案的试卷
     *
     * @param paperId paperId
     * @return ExaminationPaper
     */
    public ExaminationPaper getExaminationPaperWithAnswerById(String paperId){
        //获取指定paper（只有本身描述性信息，无小题等信息）
        ExaminationPaper result = paperRepository.getById(paperId);
        //组合小题
        result.setTopics(paperTopicService.getTopicByPaperWithAnswerId(paperId));
        return result;
    }


    /**
     * 根据用户上传的试卷，给试卷判分
     *
     * @param paperBean 用户上传的试卷
     * @return 得分
     */
    public String judgePaper(ExaminationPaper paperBean){
        //带有正确答案的试卷
        ExaminationPaper answerPaperBean = getExaminationPaperWithAnswerById(paperBean.getId());
        //计算并给出结果
        ExaminationPaper resultPaperBean = comparePaper(answerPaperBean,paperBean);
        //保存结果 todo 增加真实的uid
        paperHistoryService.savePaper(paperBean, StringsUtils.randomUUidString());
        return resultPaperBean.getUserScore();
    }

    /**
     * 与正确答案比较 并计算分数
     *
     * @param rightPaper 正确答案试卷
     * @param userPaper 用户的试卷
     * @return ExaminationPaper 结果试卷
     */
    public ExaminationPaper comparePaper(ExaminationPaper rightPaper, ExaminationPaper userPaper){
        Double userScore = 0.0;
        for (int i = 0; i < rightPaper.getTopics().size(); i++) {
            ExaminationTopic userTopicBean = userPaper.getTopics().get(i);
            ExaminationTopic rightTopicBean = rightPaper.getTopics().get(i);
            //选项
            List userOptions = Arrays.asList(userTopicBean.getOptionedIds());
            List rightOptions = Arrays.asList(rightTopicBean.getRightOptionIds());

            //如果全对 则给全部分数
            if(rightOptions.size() == userOptions.size() && rightOptions.containsAll(userOptions)){
                userScore += Double.valueOf(rightPaper.getTopics().get(i).getTopicScore());
            } else if(rightOptions.size() != userOptions.size() && !rightOptions.containsAll(userOptions)){
                //如果部分对，则给一半分数
                userScore += Double.valueOf(rightPaper.getTopics().get(i).getTopicScore()) / 2;
            }else {
                continue;
            }

            //将用户的选项封装到结果集中
            rightTopicBean.setOptionedIds((String[]) userOptions.toArray());
        }
        rightPaper.setUserScore(String.valueOf(userScore));
        return rightPaper;
    }
}
